﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

using MaxStudio.Concrete.DBUtility;

namespace MaxStudio.Concrete.Server.Dao
{
    public partial class ConcreteDaoDataContext : System.Data.Linq.DataContext
    {
        #region RPTAdmixtureAllocate

        public List<tb_RPT_AdmixtureAllocate> GetAllAdmixtureAllocate()
        {

            var query = (from obj in this.tb_RPT_AdmixtureAllocates
                         where obj.IsDelete == 0
                         select obj).ToList();
            return query;
        }
        public List<tb_RPT_AdmixtureAllocate> GetAdmixtureAllocateByAllocateDate(DateTime allocateDate)
        {

            var query = (from obj in this.tb_RPT_AdmixtureAllocates
                         where obj.IsDelete == 0 && obj.AllocateDate == allocateDate.ToShortDateString()
                         select obj).ToList();
            return query;
        }
        public List<tb_RPT_AdmixtureAllocate> GetAdmixtureAllocateByAllocateDate(DateTime allocateDate1, DateTime allocateDate2)
        {
            var query = (from obj in this.tb_RPT_AdmixtureAllocates
                         where obj.IsDelete == 0 && DateTime.Parse(obj.AllocateDate) >= allocateDate1 && DateTime.Parse(obj.AllocateDate) <= allocateDate2
                         select obj).ToList();
            return query;
        }

        public List<tb_RPT_AdmixtureAllocate> GetAdmixtureAllocateByContition(string strAllocateDate,string strPowdeDosage
                                                                            ,string strLiquidWeight,string strTankNumber
                                                                            , string strCreateDate)
        {
            var tq = from obj in this.tb_RPT_AdmixtureAllocates
                     where obj.IsDelete == 0
                     select obj;

            if (strAllocateDate.Length > 0)
            {
                tq = tq.Where(entity => entity.AllocateDate == strAllocateDate);
            }

            if (strPowdeDosage.Length > 0)
            {
                tq = tq.Where(entity => entity.PowdeDosage == strPowdeDosage);
            }
            if (strLiquidWeight.Length > 0)
            {
                tq = tq.Where(entity => entity.LiquidWeight == strLiquidWeight);
            }
            if (strTankNumber.Length > 0)
            {
                tq = tq.Where(entity => entity.TankNumber == strTankNumber);
            }
            if (strCreateDate.Length > 0)
            {
                tq = tq.Where(entity => entity.CreateDate.ToShortDateString() == strCreateDate);
            }
             
            var query = tq.ToList();
            return query;
        }

        public tb_RPT_AdmixtureAllocate GetAdmixtureAllocateById(int id)
        {
            return this.tb_RPT_AdmixtureAllocates.SingleOrDefault(tb_RPT_AdmixtureAllocates => tb_RPT_AdmixtureAllocates.Id == id);
        }

        public void CreateAdmixtureAllocate(tb_RPT_AdmixtureAllocate entity)
        {
            entity.CreateDate = DateTime.Now;
            entity.CreateBy = 1;
            entity.ModifyDate = DateTime.Now;
            entity.ModifyBy = 1;
            tb_RPT_AdmixtureAllocates.InsertOnSubmit(entity);
            this.SubmitChanges();

        }

        public void ModifyAdmixtureAllocate(tb_RPT_AdmixtureAllocate entity)
        {
            tb_RPT_AdmixtureAllocate instance = this.tb_RPT_AdmixtureAllocates.SingleOrDefault(p => p.Id == entity.Id);
            instance.AllocateDate = entity.AllocateDate;
            instance.PowdeDosage = entity.PowdeDosage;
            instance.LiquidWeight = entity.LiquidWeight;
            instance.TankNumber = entity.TankNumber;
            instance.Remark = entity.Remark;
            
            instance.IsDelete = entity.IsDelete;
            instance.ModifyBy = 1; //Need to get current user
            instance.ModifyDate = DateTime.Now;

            this.SubmitChanges();
        }

        public void DeleteAdmixtureAllocate(tb_RPT_AdmixtureAllocate entity)
        {
            tb_RPT_AdmixtureAllocate deleteEntity = tb_RPT_AdmixtureAllocates.SingleOrDefault(p => p.Id == entity.Id);
            tb_RPT_AdmixtureAllocates.DeleteOnSubmit(deleteEntity);

            this.SubmitChanges();
        }

        #endregion

    }

}
